using System;
using System.Diagnostics;
using System.IO;

using Microsoft.WowAddonStudio.Lua.Model;
using Microsoft.WowAddonStudio.Lua.Support;

using NUnit.Framework;

namespace Microsoft.WowAddonStudio.Lua.Tests.Model
{
    public class DocumentationTestBase
    {
        protected const string wowApiRelativePath = "Data/Documentation/WowAPI.xml";
        protected const string widgetApiRelativePath = "Data/Documentation/WidgetAPI.xml";
        protected const string stringRelativePath = "Data/Documentation/String.xml";

        private XmlDocumentationSerializer xmlDocumentationSerializer;

        [SetUp]
        public virtual void SetUp()
        {
            xmlDocumentationSerializer = new XmlDocumentationSerializer();
        }

        protected Documentation DeserializeDocumentation(string documentationRelativePath)
        {
            var path = Path.Combine(Environment.CurrentDirectory, documentationRelativePath);
            if (!File.Exists(path))
                Assert.Fail("Could not find XML documentation, cannot continue.");

            // Deserialize documentation
            var documentation = xmlDocumentationSerializer.Deserialize(path);

            // Merge prototypes
            documentation.MergePrototypes();

            // Return documentation
            return documentation;
        }

        protected string SerializeDocumentation(Documentation documentation)
        {
            using (var writer = new StringWriter())
            {
                xmlDocumentationSerializer.Serialize(writer, documentation);
                Trace.WriteLine(writer.ToString());
                return writer.ToString();
            }
        }

        protected void AssertDocumentationString(string xmlDocumentation)
        {
            using (var reader = new StringReader(xmlDocumentation))
            {
                // Deserialize documentation
                Assert.IsNotNull(xmlDocumentationSerializer.Deserialize(reader));
            }
        }
    }
}